package com.wx.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wx.core.process.WxApi;
import com.wx.core.vo.FansGroup;
import com.wx.dao.WxFansGroupMapper;
import com.wx.entity.WxFansGroup;
import com.wx.entity.WxFansGroupExample;
import com.wx.service.SysWxTokenService;
import com.wx.service.WxFansGroupService;
import com.zyht.common.util.HttpUtils;

@Service("WxFansGroupService")
public class WxFansGoupImpl implements WxFansGroupService {
	@Resource
	private WxFansGroupMapper wxFansGroupMapper;

	@Resource
	private SysWxTokenService sysWxTokenService;
	
	@Override
	public int insert(WxFansGroup wxFansGroup) {
		return wxFansGroupMapper.insert(wxFansGroup);
	}
	
	public List<WxFansGroup> selectByWxid(String wxid){
		WxFansGroupExample exp = new WxFansGroupExample();
		exp.createCriteria().andWxidEqualTo(wxid);
		return wxFansGroupMapper.selectByExample(exp);
	}
	@Override
	public int create(String accessToken,WxFansGroup wxFansGroup){
		String requestUrl = WxApi.getRequestUrl(WxApi.GROUP_CREATE, accessToken);
		String  json = "{\"group\":{\"name\":\""+wxFansGroup.getName()+"\"}}";
		String result = HttpUtils.httpsPost(requestUrl,json);
		JSONObject jSONObject = JSONObject.parseObject(result);
		FansGroup fansGroup = JSON.parseObject(jSONObject.getJSONObject("group").toJSONString(), FansGroup.class);
		wxFansGroup.setGroupId(Integer.valueOf(fansGroup.getId()));
		wxFansGroupMapper.insert(wxFansGroup);
		return 0;
	}
	@Override
	public int update(String accessToken,WxFansGroup record){
		String requestUrl = WxApi.getRequestUrl(WxApi.GROUP_UPDATE, accessToken);
		String  json = "{\"group\":{\"id\":"+record.getGroupId()+",\"name\":\""+record.getName()+"\"}}";
		HttpUtils.httpsPost(requestUrl,json);
		wxFansGroupMapper.updateByPrimaryKey(record);
		return 0;
	}
	@Override
	public int deleteAndInsert(String wxid,int uid ,ArrayList<FansGroup> list){
		WxFansGroupExample exp = new WxFansGroupExample();
		exp.createCriteria().andWxidEqualTo(wxid);
		wxFansGroupMapper.deleteByExample(exp);
		
		for (FansGroup fansGroup : list) {
			WxFansGroup wxFansGroup = new WxFansGroup();
			wxFansGroup.setCount(Integer.valueOf(fansGroup.getCount()));
			wxFansGroup.setCreateTime(new Date());
			wxFansGroup.setUpdateTime(new Date());
			wxFansGroup.setGroupId(Integer.valueOf(fansGroup.getId()));
			wxFansGroup.setName(fansGroup.getName());
			wxFansGroup.setUid(uid);
			wxFansGroup.setWxid(wxid);
			wxFansGroupMapper.insert(wxFansGroup);
		}
		return 0;
	}
	@Override
	public List<WxFansGroup> selectByExample(WxFansGroupExample example) {
		return wxFansGroupMapper.selectByExample(example);
	}
	@Override
	public WxFansGroup selectByPrimaryKey(Integer id) {
		return wxFansGroupMapper.selectByPrimaryKey(id);
	}
	@Override
	public int countByExample(WxFansGroupExample example) {
		return wxFansGroupMapper.countByExample(example);
	}
	@Override
	public int updateByExample(WxFansGroup record, WxFansGroupExample example) {
		return wxFansGroupMapper.updateByExample(record, example);
	}
	@Override
	public int updateByPrimaryKey(WxFansGroup record) {
		return wxFansGroupMapper.updateByPrimaryKey(record);
	}
	@Override
	public int deleteByPrimaryKey(Integer id) {
		return wxFansGroupMapper.deleteByPrimaryKey(id);
	}
	
	/**
	 * 根据微信端groupid删除组
	 */
	@Override
	public int deleteByGroupId(String accessToken,WxFansGroup wxFansGroup) {
		String requestUrl = WxApi.getRequestUrl(WxApi.GROUP_DELETE, accessToken);
		String  json = "{\"group\":{\"id\":"+wxFansGroup.getGroupId()+"\"}}";
		HttpUtils.httpsPost(requestUrl,json);
		return wxFansGroupMapper.deleteByPrimaryKey(wxFansGroup.getId());
	}

	@Override
	public int deleteByPrimaryKey(WxFansGroupExample example) {
		return wxFansGroupMapper.deleteByExample(example);
	}
}
